package mt.demo.service.comm;

import mt.demo.domain.comm.Spittle;
import org.apache.activemq.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import java.io.Serializable;

/**
 * Created by watson on 2015/12/18.
 */
@Component
public class AlertServiceImpl implements AlertService{
    @Autowired
    JmsTemplate jmsTemplatel;
    //发送消息到目的地
    public void sendSpittleAlert(final Spittle spittle){
        jmsTemplatel.send("spittle.alert.queue",
                session ->
                        session.createObjectMessage((Serializable) spittle));
    }
    //接收消息
    public Spittle getAlert(){
        try {
            ObjectMessage receivedMessage = (ObjectMessage) jmsTemplatel.receive();
            return (Spittle) receivedMessage.getObject();
        }catch (JMSException jmsException){
            throw JmsUtils.convertJmsAccessException(jmsException);
        }
    }
}
